package com.xuecheng.manage_cms.controller;

import com.xuecheng.api.cms.CmsStaticControllerApi;
import com.xuecheng.framework.model.response.ResponseResult;
import com.xuecheng.framework.web.BaseController;
import com.xuecheng.manage_cms.service.CmsConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;

@Controller
@RequestMapping("/cmsStatic")
public class CmsStaticController extends BaseController implements CmsStaticControllerApi {
    @Autowired
    private CmsConfigService cmsConfigService;

    /**
     * @Description: 静态化页面进行预览
     * @Param:
     * @return:
     * @Author: Mr.Yuan
     * @Date:
     */
    @GetMapping("/find/{id}")
    @Override
    public void findByPageId(@PathVariable("id") String id) {

        String html = cmsConfigService.getHtml(id);
        response.setContentType("text/html;charset=utf-8");
        try {
            response.getWriter().write(html);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @PostMapping("/publish/{id}")
    public ResponseResult publish(@PathVariable("id") String id) {
        return cmsConfigService.publishHtml(id);
    }
}
